package com.ruoyi.lhky.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.lhky.mapper.StudyCompletionMapper;
import com.ruoyi.lhky.pojo.StudyCompletion;
import com.ruoyi.lhky.util.R;
import org.springframework.stereotype.Service;

@Service
public class StudyCompletionService extends ServiceImpl<StudyCompletionMapper, StudyCompletion> {

    public R saveOrUpdateStudyCompletion(StudyCompletion studyCompletion) {
        Long testerId = studyCompletion.getTesterId();

        StudyCompletion existingData = getOne(new QueryWrapper<StudyCompletion>().eq("tester_id", testerId));

        if (existingData != null) {
            studyCompletion.setId(existingData.getId());
        }

        boolean result = saveOrUpdate(studyCompletion);
        return new R(result, result ? "保存成功" : "保存失败");
    }

    public R getStudyCompletionByTesterId(Long testerId) {
        StudyCompletion data = getOne(new QueryWrapper<StudyCompletion>().eq("tester_id", testerId));
        if (data != null) {
            return new R(true, data);
        } else {
            return new R(false, "记录不存在");
        }
    }
}
